package com.cx.base.autoReturn.utils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import com.cx.base.apiModel.ApiModel;
import com.cx.base.autoDoc.annotations.AutoDocMethod;
import com.cx.base.autoDoc.utils.ClassRefUtil;

/**
 * 返回模拟数据工具
 * cenxi 2017-01-12
 * */
public class AutoReturnUtil {

	/**
	 * 返回模拟数据
	 * 
	 * @param clazz controller类
	 * @param methodName  方法名
	 * @return
	 */
	public static ApiModel getSuccApiModel(Class clazz,  String methodName){
		ApiModel ret = new ApiModel();
		ret.setStatus("0");
		ret.setErrorMsg("操作成功");
		ret.setBody(null);
		
		//实例化一个对象
		try {
			if(clazz != null){
				Method[] methods = clazz.getMethods();
				if(methods != null && methods.length > 0){
					for(Method method : methods){
						
						AutoDocMethod autoDocMethod = method.getAnnotation(AutoDocMethod.class);
						if(autoDocMethod != null){
							Class resClazz = autoDocMethod.resClazz();
							Object resObj = resClazz.newInstance();
							resObj = ClassRefUtil.setFieldValue(resObj);
							
							//判断是否是集合
							if(autoDocMethod.resSet() != null && "list".equals(autoDocMethod.resSet())){
								List<Object> resList = new ArrayList<Object>();
								resList.add(resObj);
								resList.add(resObj);
								ret.setBody(resList);
							}else{
								ret.setBody(resObj);
							}
						}
					}
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		return ret;
	}
}
